package hud;

import event.EventBase;
import event.EventEnum;
import event.EventSystem;
import event.ListenerInterface;
import hud.data.CollectionInformation;
import javafx.geometry.Insets;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;

public class CollectionView implements ListenerInterface {
    private static CollectionView instance;
    private TabPane tabPane;

    private CollectionView() {

    }
    public static CollectionView instance() {
        if (instance == null) {
            instance = new CollectionView();
        }
        return instance;
    }

    public VBox init(VBox viewBox) {
        VBox rightVBox = new VBox();
        rightVBox.setPadding(new Insets(0));
        rightVBox.setSpacing(0);
        rightVBox.prefHeightProperty().bind(viewBox.heightProperty());

        tabPane = new TabPane();
        tabPane.prefHeightProperty().bind(rightVBox.heightProperty());
        rightVBox.getChildren().add(tabPane);

        EventSystem.instance().on(EventEnum.closeAllTab, this);
        return rightVBox;
    }

    public void newTab(CollectionInformation ci) {
        Tab tab = new CollectionTab().init(tabPane, ci);
        tabPane.getTabs().add(tab);
        tabPane.getSelectionModel().select(tab);
    }

    @Override
    public void process(EventBase event) {
        if (event.getName() == EventEnum.closeAllTab) {
            tabPane.getTabs().removeAll(tabPane.getTabs());
        }
    }
}
